<template>
	<view class="page">
		<view class="page-box">
			<view class="btnBox">
				<view class="title">
					<image class="tex" src="@/static/logo.png" mode=""></image>
					<view class="name">重置密码</view>
				</view>
				<view class="choose-nickname-row">
					<input v-model="form.oldpassword" type="text" placeholder="请输入旧密码" password @blur="checkOld"
						ref="inputVal" />
				</view>
				<view class="choose-nickname-row">
					<input v-model="form.newpassword" type="text" placeholder="请设置新密码" password @blur="checkNew"
						ref="inputVal" />
				</view>
				<view class="choose-nickname-row">
					<input v-model="form.confirmCode" type="text" placeholder="请再次输入新密码" password @blur="checkTwice"
						ref="inputVal" />
				</view>
			</view>
			<view class="" style="width: 100%;">
				<But @tap.stop="handleSubmit" text="确认"></But>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		resetpwd
	} from "@/request/myApi.js";
	import But from "@/components/Button/index.vue"
	export default {
		components: {
			But,
		},
		data() {
			return {
				form: {
					mobile: '',
					oldpassword: '',
					newpassword: '',
					confirmCode: '',
				},
			};
		},
		onLoad() {
			this.form.mobile = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")).mobile : '';
		},
		methods: {
			// 输入旧密码
			checkOld() {
				if (this.form.oldpassword == '') {
					uni.showToast({
						title: '请输入旧密码',
						duration: 1500,
						icon: 'none',
					})
					return false
				} else if (this.form.oldpassword.length < 6) {
					uni.showToast({
						title: '密码大于等于六位',
						duration: 1500,
						icon: 'none',
					})
					return false
				} else {
					return true;
				}
			},

			// 输入新密码
			checkNew() {
				if (this.form.newpassword == '') {
					uni.showToast({
						title: '请输入新密码',
						duration: 1500,
						icon: 'none',
					})
					return false
				} else if (this.form.newpassword.length < 6) {
					uni.showToast({
						title: '密码大于等于六位',
						duration: 1500,
						icon: 'none',
					})
					return false
				} else {
					return true;
				}
			},

			// 确认密码
			checkTwice() {
				console.log(this.form.confirmCode, 'this.form.confirmCode')
				if (this.form.confirmCode == '') {
					uni.showToast({
						title: '请确认密码',
						duration: 1500,
						icon: 'none',
					})
					return false
				} else if (this.form.confirmCode.length < 6) {
					uni.showToast({
						title: '密码大于等于六位',
						duration: 1500,
						icon: 'none',
					})
					return false
				} else if (this.form.newpassword != this.form.confirmCode) {
					uni.showToast({
						title: '两次密码不一致',
						icon: "none",
					});
					return false
				} else {
					return true;
				}
			},

			// 提交修改
			handleSubmit() {
				setTimeout(() => {
					this.$refs.inputVal.blur()
				}, 500);
				setTimeout(() => {
					if (!this.checkTwice()) return;
					if (!this.checkNew()) return;
					if (!this.checkOld()) return;

					delete this.form.confirmCode
					resetpwd(this.form).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: res.msg,
								duration: 1500,
								icon: "none",
							});
							uni.removeStorageSync('userInfo')
							uni.removeStorageSync('token')
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/my/success?title=重置完成'
								})
							}, 1500);
						} else {
							uni.showToast({
								title: res.msg,
								duration: 1500,
								icon: "none",
							});
						}
					});
				}, 1000)


				// if(this.form.confirmCode == ''){
				// 	uni.showToast({
				// 		title: '请输入旧密码',
				// 		duration: 1500,
				// 		icon: 'none',
				// 	})
				// 	return
				// }else
			},
		},
	};
</script>

<style lang="scss" scoped>
	button::after {
		border: none;
		background-color: transparent !important;
		padding: 0;
	}

	.page {
		background-image: url("");
		background-size: 100% 100%;
		height: 100%;
		background-repeat: no-repeat;
		position: fixed;
		top: 0;
		overflow: hidden;
		left: 0;
		width: 100%;

		&-box {
			height: 800rpx;
			margin: 260rpx auto;
			width: 640rpx;
			display: flex;
			flex-direction: column;
			justify-items: center;
		}
	}

	.btnBox {
		width: 100%;
		height: 600rpx;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;

		.title {
			display: flex;
			align-items: center;
			height: 100rpx;
			width: 46%;
			justify-content: space-between;
			margin-bottom: 50rpx;

			.tex {
				width: 100rpx;
				height: 100%;
			}

			.name {
				font-size: 42rpx;
				color: #fff;
			}
		}


		.choose-nickname-row {
			width: 100%;
			height: 9%;
			padding: 62rpx 4rpx 7rpx;
			text-align: left;
			font-size: 30rpx;
			margin: 0 auto;
			border-bottom: 1px solid #F2F2F2;

			.input {
				color: #807E7E;
			}
		}
	}
</style>